package pl.prv.polanie.client.widgets;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Klasa umozliwiajaca sortowanie obiektow na podstawie dowolnie wybranych w nich polach.
 * 
 * @author Janusz Prokulewicz
 * Created: Nov 29, 2008
 */
public class Sorter {
	
	
	List<StringKeyWithObject> listWithStringKeys = new ArrayList<StringKeyWithObject>();
	List<LongKeyWithObject> listWithLongKeys = new ArrayList<LongKeyWithObject>();
	
	
	public void addItem(String label, Object object) {
		StringKeyWithObject skwo = new StringKeyWithObject(label, object);
		listWithStringKeys.add(skwo);
	}
	
	public void addItem(Long label, Object object) {
		LongKeyWithObject lkwo = new LongKeyWithObject(label, object);
		listWithLongKeys.add(lkwo);
	}

	public void sortWithAlphabeticalOrder() {
		if (listWithStringKeys.size() > 0) {
			Collections.sort(listWithStringKeys);
		}
		
		if (listWithLongKeys.size() > 0) {
			Collections.sort(listWithLongKeys);
		}
	}

	/**
	 * Zwraca liste z obiektami dodanymi uprzednio do obiektu klasy Sorter.
	 * @return
	 */
	public List<Object> getListWithObjects() {
		List<Object> result = new ArrayList<Object>();
		
		if (listWithStringKeys.size() > 0) {
			for (int i = 0; i < listWithStringKeys.size(); i++) {
				StringKeyWithObject skwo = listWithStringKeys.get(i);
				result.add(skwo.getObject());
			}
		} else if (listWithLongKeys.size() > 0) {
			for (int i = 0; i < listWithLongKeys.size(); i++) {
				LongKeyWithObject lkwo = listWithLongKeys.get(i);
				result.add(lkwo.getObject());
			}
		}
		
		return result;
	}
	
//	public Object[] getArrayWithObjects() {
//		List<Object> list = getListWithObjects();
//		Object[] objects = new Object[list.size()];
//		for (int i = 0; i < objects.length; i++) {
//			objects[i] = list.get(i);
//		}
//		return objects;
//	}
//	
	
	
	
	class StringKeyWithObject implements Comparable<StringKeyWithObject> {
		String label;
		Object object;
		
 		public StringKeyWithObject(String label, Object object) {
			this.label = label;
			this.object = object;
		}

 		public int compareTo(StringKeyWithObject o) {
 			return this.label.compareToIgnoreCase(o.label);
 		}

		public String getLabel() {
			return label;
		}

		public void setLabel(String label) {
			this.label = label;
		}

		public Object getObject() {
			return object;
		}

		public void setObject(Object object) {
			this.object = object;
		}
	};
	
	class LongKeyWithObject implements Comparable<LongKeyWithObject> {
		Long label;
		Object object;
		
 		public LongKeyWithObject(Long label, Object object) {
			this.label = label;
			this.object = object;
		}

 		public int compareTo(LongKeyWithObject o) {
 			return this.label.compareTo(o.label);
 		}

		public Long getLabel() {
			return label;
		}

		public void setLabel(Long label) {
			this.label = label;
		}

		public Object getObject() {
			return object;
		}

		public void setObject(Object object) {
			this.object = object;
		}
	};

}
